package com.fdb.efp.nls.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fdb.basic.framework.core.base.AbstractFrameworkService;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.SequenceConstance;
import com.fdb.efp.nls.service.dao.*;
import com.fdb.efp.nls.service.domain.PspCheckTaskApp;
import com.fdb.efp.nls.service.domain.PspSpecCheckApp;
import com.fdb.efp.nls.service.domain.PspSpecCheckCont;
import com.fdb.efp.nls.service.facade.PspSpecCheckAppService;
import com.fdb.efp.nls.service.vo.PspSpecCheckAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 专项检查申请表				<p>
 * @author luzy
 * @date 2019-09-13
 */
@Service("pspSpecCheckAppService")
public class PspSpecCheckAppServiceImpl extends AbstractFrameworkService implements PspSpecCheckAppService, FrameworkService {
	
	private static final PspSpecCheckCont PspSpecCheckContVO = null;

	private static Logger logger = LoggerFactory.getLogger(PspSpecCheckAppServiceImpl.class);

	@Autowired
	private PspSpecCheckAppDao pspSpecCheckAppDao;
	@Autowired
	private PspSpecCheckContDao pspSpecCheckContDao;
	@Autowired
	private PspCheckTaskAppDao pspCheckTaskAppDao;
	@Autowired
	private PspOverdueBillInfoDao pspOverdueBillInfoDao;
	@Autowired
	private PspBillListDao pspBillListDao;
	@Autowired
	private PspWarnInfoDao pspWarnInfoDao;
	
	
	
	/**
	 * 新增操作
	 */
	public int insertPspSpecCheckApp(PspSpecCheckAppVO inPspSpecCheckAppVo) {
		logger.debug("当前新增数据为:"+ inPspSpecCheckAppVo.toString());
		int num = 0;
		try {
			PspSpecCheckApp pspSpecCheckApp = new PspSpecCheckApp();
			beanCopy(inPspSpecCheckAppVo, pspSpecCheckApp);

//			pspSpecCheckApp.setBizSerno(SequenceConstance.PSP_BIZ_SERNO+currentTimeMillis());
//			pspSpecCheckApp.setSpecCheckStatus(RiskwarningConstant.SPEC_CHECK_STATUS_00);
////			pspSpecCheckApp.setChkType(RiskwarningConstant.S_ZB_CHK_TYPE_01);
			num = pspSpecCheckAppDao.insertPspSpecCheckApp(pspSpecCheckApp);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(PspSpecCheckAppVO inPspSpecCheckAppVo) {
		logger.debug("当前删除数据条件为:"+ inPspSpecCheckAppVo);
		int num = 0;
		try {
			PspSpecCheckApp pspSpecCheckApp = new PspSpecCheckApp();
			beanCopy(inPspSpecCheckAppVo, pspSpecCheckApp);
			num = pspSpecCheckAppDao.deleteByPk(pspSpecCheckApp);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPspSpecCheckAppVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(PspSpecCheckAppVO inPspSpecCheckAppVo) {
		logger.debug("当前修改数据为:"+ inPspSpecCheckAppVo.toString());
		int num = 0;
		try {
			PspSpecCheckApp pspSpecCheckApp = new PspSpecCheckApp();
			beanCopy(inPspSpecCheckAppVo, pspSpecCheckApp);
			num = pspSpecCheckAppDao.updateByPk(pspSpecCheckApp);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPspSpecCheckAppVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public PspSpecCheckAppVO queryByPk(PspSpecCheckAppVO inPspSpecCheckAppVo) {
		
		logger.debug("当前查询参数信息为:"+ inPspSpecCheckAppVo);
		try {
			PspSpecCheckApp queryPspSpecCheckApp = new PspSpecCheckApp();
			beanCopy(inPspSpecCheckAppVo, queryPspSpecCheckApp);
			PspSpecCheckApp queryRslPspSpecCheckApp = pspSpecCheckAppDao.queryByPk(queryPspSpecCheckApp);
			if (Objects.nonNull(queryRslPspSpecCheckApp)) {
				PspSpecCheckAppVO outPspSpecCheckAppVo = beanCopy(queryRslPspSpecCheckApp, new PspSpecCheckAppVO());
				logger.debug("当前查询结果为:"+ outPspSpecCheckAppVo.toString());
				return outPspSpecCheckAppVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PspSpecCheckAppVO> queryAllOwner(PspSpecCheckAppVO pspSpecCheckAppVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		PspSpecCheckApp pspSpecCheckApp = beanCopy(pspSpecCheckAppVo, new PspSpecCheckApp());
		List<PspSpecCheckAppVO> list = null;
		try {
			List<PspSpecCheckApp> pspSpecCheckApps = pspSpecCheckAppDao.queryAllOwnerByPage(pspSpecCheckApp);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ pspSpecCheckApps.size());
			pageSet(pspSpecCheckApps, pspSpecCheckApp);
			list = (List<PspSpecCheckAppVO>) beansCopy(pspSpecCheckApps, PspSpecCheckAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PspSpecCheckAppVO> queryAllCurrOrg(PspSpecCheckAppVO pspSpecCheckAppVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		PspSpecCheckApp pspSpecCheckApp = beanCopy(pspSpecCheckAppVo, new PspSpecCheckApp());
		List<PspSpecCheckApp> pspSpecCheckApps = pspSpecCheckAppDao.queryAllCurrOrgByPage(pspSpecCheckApp);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+pspSpecCheckApps.size());
		List<PspSpecCheckAppVO> list = null;
		try {
			pageSet(pspSpecCheckApps, pspSpecCheckApp);
			list = (List<PspSpecCheckAppVO>) beansCopy(pspSpecCheckApps, PspSpecCheckAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PspSpecCheckAppVO> queryAllCurrDownOrg(PspSpecCheckAppVO pspSpecCheckAppVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		PspSpecCheckApp pspSpecCheckApp = beanCopy(pspSpecCheckAppVo, new PspSpecCheckApp());
		List<PspSpecCheckApp> pspSpecCheckApps = pspSpecCheckAppDao.queryAllCurrDownOrgByPage(pspSpecCheckApp);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ pspSpecCheckApps.size());
		List<PspSpecCheckAppVO> list = null;
		try {
			pageSet(pspSpecCheckApps, pspSpecCheckApp);
			list = (List<PspSpecCheckAppVO>) beansCopy(pspSpecCheckApps, PspSpecCheckAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	/**
	 * 提交
	 */
	@Override
	public int SubmitByPk(PspSpecCheckAppVO pspSpecCheckAppVo) {
		logger.info("批量新增数据："+JSONObject.toJSONString(pspSpecCheckAppVo));
		int num = -1;
		PspCheckTaskApp pspCheckTaskApp = new PspCheckTaskApp();
		List<PspCheckTaskApp> appList =new  ArrayList<PspCheckTaskApp>();
		try {
			String bizSerno = pspSpecCheckAppVo.getBizSerno();
			String pspBizSerno=this.getSequence(SequenceConstance.PSP_BIZ_SERNO);
			logger.info("生成流水号："+pspBizSerno);
			List<PspSpecCheckCont> list = pspSpecCheckContDao.querySerno(bizSerno);
			for (int i=0;i<=list.size();i++) { 
				PspSpecCheckCont pspSpecCheckCont=list.get(i);
				beanCopy(pspSpecCheckCont, pspCheckTaskApp);
				pspCheckTaskApp.setBizSerno(pspBizSerno);
				appList.add(pspCheckTaskApp);
				//num = pspCheckTaskAppDao.insertPspCheckTaskApp(pspCheckTaskApp);
			}
			if(Objects.nonNull(appList) && appList.size()>0) {
				logger.info("批量新增数据传入参数："+JSONObject.toJSONString(appList));
				num = pspCheckTaskAppDao.insertBatchPspCheckTaskApp(appList);
			}
			
		} catch (Exception e) {
			logger.error("提交数据发生异常!", e);
		}
		return num;
	}

	@Override
	public List<PspSpecCheckAppVO> querySignSearchAll(PspSpecCheckAppVO pspSpecCheckAppVo) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<PspSpecCheckAppVO> list = pspSpecCheckAppDao.querySignSearchAllByPk(pspSpecCheckAppVo);;
		return list;
	}

	@Override
	public List<PspSpecCheckAppVO> queryExAllOwner(PspSpecCheckAppVO pspSpecCheckAppVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		PspSpecCheckApp pspSpecCheckApp = beanCopy(pspSpecCheckAppVo, new PspSpecCheckApp());
		List<PspSpecCheckAppVO> list = null;
		try {
			List<PspSpecCheckApp> pspSpecCheckApps = pspSpecCheckAppDao.queryAllOwner(pspSpecCheckApp);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ pspSpecCheckApps.size());
			pageSet(pspSpecCheckApps, pspSpecCheckApp);
			list = (List<PspSpecCheckAppVO>) beansCopy(pspSpecCheckApps, PspSpecCheckAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构权限数据
	 */
	@Override
	public List<PspSpecCheckAppVO> queryExAllCurrOrg(PspSpecCheckAppVO pspSpecCheckAppVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		PspSpecCheckApp pspSpecCheckApp = beanCopy(pspSpecCheckAppVo, new PspSpecCheckApp());
		List<PspSpecCheckApp> pspSpecCheckApps = pspSpecCheckAppDao.queryAllCurrOrg(pspSpecCheckApp);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+pspSpecCheckApps.size());
		List<PspSpecCheckAppVO> list = null;
		try {
			pageSet(pspSpecCheckApps, pspSpecCheckApp);
			list = (List<PspSpecCheckAppVO>) beansCopy(pspSpecCheckApps, PspSpecCheckAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	public List<PspSpecCheckAppVO> queryExAllCurrDownOrg(PspSpecCheckAppVO pspSpecCheckAppVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		PspSpecCheckApp pspSpecCheckApp = beanCopy(pspSpecCheckAppVo, new PspSpecCheckApp());
		List<PspSpecCheckApp> pspSpecCheckApps = pspSpecCheckAppDao.queryAllCurrDownOrg(pspSpecCheckApp);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ pspSpecCheckApps.size());
		List<PspSpecCheckAppVO> list = null;
		try {
			pageSet(pspSpecCheckApps, pspSpecCheckApp);
			list = (List<PspSpecCheckAppVO>) beansCopy(pspSpecCheckApps, PspSpecCheckAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

}
